package com.cbgolf.oa.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.entity.ShopBean;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lq on 2017/10/31.
 */

public class LeftAdapter extends BaseAdapter {
    private List<ShopBean> list;
    private LayoutInflater mInflater;
    private Context context;
    private Map<Integer, Boolean> map = new HashMap<>();

    public LeftAdapter(Context context, IClickListener listener) {
        mInflater = LayoutInflater.from(context);
        this.listener = listener;
        list = new ArrayList<>();
        this.context = context;
    }

    public void clear() {
        if (!Util.listIsNull(list)) {
            this.list.clear();
        }
    }

    public void setList(List<ShopBean> list) {
        this.list = list;

        for (int i = 0; i < list.size(); i++) {
            map.put(i, (i == 0));
        }
        this.notifyDataSetChanged();
    }

    public void setSelect(int index) {
        if (DataUtil.mapIsNull(map)) return;
//        if (DataUtil.getMapBoolean(map, index)) return;
        for (int i = 0; i < map.size(); i++) {
            map.put(i, (index == i));
        }
        this.notifyDataSetChanged();
    }

    public void addData(List<ShopBean> list) {
        this.list.addAll(list);
    }

    public void upDataUI() {
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        Holder h = null;
        if (null == view) {
            h = new Holder();
            view = mInflater.inflate(R.layout.item_left, null);
            h.item = view.findViewById(R.id.item_left_layout_ll);
            h.iv = (ImageView) view.findViewById(R.id.item_left_iv);
            h.tv = (TextView) view.findViewById(R.id.item_left_tv);
            AutoUtil.auto(view);
            view.setTag(h);
        } else {
            h = (Holder) view.getTag();
        }
//        h.iv.setImageResource(list.get(position).resId);

        h.tv.setText(TextUtil.Text(list.get(position).goodsTypeName));
        h.item.setBackgroundColor(ContextCompat.getColor(context, DataUtil.getMapBoolean(map, position) ? R.color.white : R.color.transparent));
        h.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < map.size(); i++) {
                    map.put(i, false);
                }
                map.put(position, true);
                upDataUI();
                if (null != listener) listener.click(position);
            }
        });

        return view;
    }

    class Holder {
        private View item;
        private ImageView iv;
        private TextView tv;

    }

    private IClickListener listener;

    public interface IClickListener {
        void click(int position);
    }

}

